﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WeChatSDK.Core
{
    public class CommunicateHelper
    {
        #region 初始化
        /// <summary>
        /// 
        /// </summary>
        private static CommunicateHelper helper = null;
        /// <summary>
        /// 
        /// </summary>
        private CommunicateHelper() { }
        /// <summary>
        /// 使用前请在config文件加上 wx_appid 和 wx_secret
        /// </summary>
        /// <returns></returns>
        public static CommunicateHelper init()
        {
            if (helper == null)
            {
                helper = new CommunicateHelper();
                helper.AppID = System.Configuration.ConfigurationSettings.AppSettings["wx_appid"];
                helper.AppSecret = System.Configuration.ConfigurationSettings.AppSettings["wx_secret"];

            }
            return helper;
        }
        #endregion

        #region 参数
        /// <summary>
        /// 
        /// </summary>
        public string AppID { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public string AppSecret { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime ExpiresIn { get; set; }
        #endregion

        #region 方法
        /// <summary>
        /// 用GET请求获取数据
        /// </summary>
        /// <returns></returns>
        public string HttpGet(string url)
        {
            string rec = string.Empty;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "GET";
            using (WebResponse rsp = req.GetResponse())
            {
                using (StreamReader sr = new StreamReader(rsp.GetResponseStream(), Encoding.UTF8))
                {
                    rec = sr.ReadToEnd();
                }
            }
            return rec;
        }
        /// <summary>
        /// 用POST方式获取数据
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <param name="data">发起请求的参数</param>
        /// <param name="header">发起请求的包头</param>
        /// <returns></returns>
        public string HttpPost(string url, string data = "", string content_type = "application/x-www-form-urlencoded", string header = "")
        {
            string rec = string.Empty;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "POST";

            req.ContentType = content_type;

            byte[] postBytes = null;
            if (!string.IsNullOrEmpty(data))
            {
                postBytes = Encoding.ASCII.GetBytes(data);
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(postBytes, 0, postBytes.Length);
                }
            }

            using (WebResponse rsp = req.GetResponse())
            {
                using (StreamReader sr = new StreamReader(rsp.GetResponseStream(), Encoding.UTF8))
                {
                    rec = sr.ReadToEnd();
                }
            }

            return rec;
        }

        #endregion
    }
}
